package com.controller;

import com.model._do.DeSignDO;
import com.model.dto.baseDTO.ResultDTO;
import com.service.AppointmentService;
import com.service.DeSignUpService;
import com.service.ResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/design")
public class DeSignController {

    @Autowired
    private ResultService resultService;

    @Autowired
    private DeSignUpService deSignUpService;

    @Autowired
    private AppointmentService appointmentService;

    /**
     * 添加入新的解约表元组
     * @param deSign from，to
     * @return 成功提示
     */
    @RequestMapping(value = "/addDeSign", method = RequestMethod.POST)
    public ResultDTO addDeSign(@RequestBody DeSignDO deSign){
        try{
            if(appointmentService.getAppointIfExist(deSign.getFrom(), deSign.getTo())
                    .size() > 0){
                return resultService.createFailed("still has no timeout appoint");
            }
            deSignUpService.addNewDeSign(deSign);
            return resultService.createSuccess("success");
        } catch (Exception e){
            return resultService.createFailed("failed");
        }
    }

    /**
     * 根据userId，equipId获取解约表元组
     * @param design userId，equipId
     * @return 解约表有效元组 avili = 1 or avili is null
     */
    @RequestMapping(value = "/getDesignByUserEquip")
    public ResultDTO getDesign(@RequestBody DeSignDO design){
        return resultService.createSuccess("success",deSignUpService.getDeSignByFromToId(design.getFrom(), design.getTo()));
    }

    /**
     * 通过id 将design的agreement置为1
     * @param deSign id
     * @return 成功标识
     */
    @RequestMapping(value = "/confirmDeSign", method = RequestMethod.POST)
    public ResultDTO confirmDeSign(@RequestBody DeSignDO deSign){
        deSignUpService.confirmDeSign(deSign);
        return resultService.createSuccess("success");
    }

    /**
     * 通过design id将design的avili置为0
     * @param deSign id
     * @return 成功标识
     */
    @RequestMapping(value = "/deleteDeSign", method = RequestMethod.POST)
    public ResultDTO deleteDeSign(@RequestBody DeSignDO deSign){
        try {
            deSignUpService.deleteDeSign(deSign);
            return resultService.createSuccess("success");
        } catch (Exception e){
            return resultService.createFailed("failed");
        }
    }
}
